package com.cloudspace.yunloan.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;

import com.cloudspace.yunloan.R;
import com.cloudspace.yunloan.adapter.FinishCaseAdapter;
import com.cloudspace.yunloan.entity.CaseEntity;
import com.cloudspace.yunloan.entity.UnderwayListResult;
import com.cloudspace.yunloan.network.JsonCallBack;
import com.cloudspace.yunloan.network.api.API;
import com.cloudspace.yunloan.utils.ObjUtil;
import com.cloudspace.yunloan.yinterface.OnFinishCaseClickInterface;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;


public class CaseFinishedFragment extends BaseFragment {
    private static final String ARG_SECTION_NUMBER = "section_number";

    private View rootView;

    private GridView mGridView;
    private FinishCaseAdapter mAdapter;

    private List<CaseEntity> mData = new ArrayList<>();
    private OnFinishCaseClickInterface finishCaseClickInterface;

    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_case_finish, container, false);

        mGridView = rootView.findViewById(R.id.case_finish);
        mAdapter = new FinishCaseAdapter();
        mGridView.setAdapter(mAdapter);
        //已完成项目不能点
//        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                if (finishCaseClickInterface != null){
//                    finishCaseClickInterface.onCaseClick(mData.get(position));
//                }
//            }
//        });

        return rootView;
    }

    @Override
    public void onResume() {
        super.onResume();

        getData();
    }

    public static final String TAG = CaseFinishedFragment.class.getSimpleName();

    @Override
    public void onFragmentCreated(Bundle savedInstanceState) {

    }

    @Override
    public void updateContent() {
    }

    private void getData(){
        API.getInstance().getUserApi().finished(getActivity(), new JsonCallBack<UnderwayListResult>() {
            @Override
            public void onSuccess(UnderwayListResult underwayListResult, Call call, Response response) {
                mData.clear();
                if (underwayListResult != null && !ObjUtil.isEmpty(underwayListResult.data)){
                    mData.addAll(underwayListResult.data);
                }

                mAdapter.setData(mData);
            }
        });
    }


    public void setFinishCaseClickInterface(OnFinishCaseClickInterface finishCaseClickInterface) {
        this.finishCaseClickInterface = finishCaseClickInterface;
    }
}
